package com.whz.iplocation.ipregion.search.db;

import com.whz.iplocation.gzip.ZlibCompression;
import com.whz.iplocation.http.HttpClientUtils;
import lombok.Data;

/**
 * 获取ip数据库并且解密解压缩
 *
 * @author whz
 */
@Data
public class IpDataLoader {
    public static final IpDataLoader DEFAULT_LOADER = new IpDataLoader();
    public static final String COPY_WRITE_RAR_LINK = "http://update.cz88.net/ip/copywrite.rar";
    public static final String QQ_WRY_RAR_LINK = "http://update.cz88.net/ip/qqwry.rar";

    private String cwLink;
    private String dataLink;
    private CwFile ipCwFile;
    private CwFileAnalyzer fileAnalyzer;


    public IpDataLoader() {
        this(COPY_WRITE_RAR_LINK, QQ_WRY_RAR_LINK);
    }

    public IpDataLoader(final String cwLink, final String dataLink) {
        this.cwLink = cwLink;
        this.dataLink = dataLink;
        this.ipCwFile = new CwFile(HttpClientUtils.get(this.cwLink));
        this.fileAnalyzer = new CwFileAnalyzer(this.ipCwFile.getSecret());
    }

    public byte[] getDataBase() {
        byte[] d = this.fileAnalyzer.decode(HttpClientUtils.get(QQ_WRY_RAR_LINK));
        return ZlibCompression.decompressFile(d);
    }

}